<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="content" label="内容">
				<uni-easyinput v-model="formData.content" />
			</uni-forms-item>
			<uni-forms-item name="reply" label="回复">
				<uni-easyinput type="textarea" v-model="formData.reply" />
			</uni-forms-item>
			<uni-forms-item name="goods_id" label="商品">
				<uni-data-checkbox v-model="formData.goods_id" collection="cloud_goods"
					field="title as text, _id as value" />
			</uni-forms-item>
			<uni-forms-item name="index" label="索引">
				<uni-easyinput type="number" v-model="formData.index" />
			</uni-forms-item>
			<uni-forms-item name="order_id" label="订单id">
				<uni-easyinput v-model="formData.order_id" />
			</uni-forms-item>
			<uni-forms-item name="shopid" label="店铺">
				<uni-data-checkbox v-model="formData.shopid" collection="cloud_shops"
					field="name as text, id as value" />
			</uni-forms-item>
			<uni-forms-item name="star" label="评分">
				<uni-easyinput type="number" v-model="formData.star" />
			</uni-forms-item>
			<uni-forms-item name="starResult" label="评分内容">
				<uni-easyinput v-model="formData.starResult" />
			</uni-forms-item>
			<uni-forms-item name="tags" label="标签">
				<uni-data-checkbox :multiple="true" v-model="formData.tags" />
			</uni-forms-item>

			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/cloud_goods_comments.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'cloud_goods_comments';

	function getValidator(fields) {
		let reuslt = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				reuslt[key] = validator[key]
			}
		}
		return reuslt
	}

	export default {
		data() {
			return {
				formData: {
					"content": "",
					"reply": "",
					"goods_id": "",
					"index": null,
					"order_id": "",
					"shopid": null,
					"star": null,
					"starResult": "",
					"tags": [],
					"user": null
				},
				formOptions: {},
				rules: {
					...getValidator(["content", "reply", "goods_id", "index", "order_id", "shopid", "star", "starResult",
						"tags", "user"
					])
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.submit().then((res) => {
					this.submitForm(res)
				}).catch((errors) => {
					uni.hideLoading()
				})
			},

			submitForm(value) {
				// 使用 clientDB 提交数据
				db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>
